package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.AdSensitive;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

@Service
@Transactional
@Slf4j
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {
    /**
     * 查询所有的敏感词
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findList(SensitiveDto dto) {
        //检查参数
        if (dto == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        //分页参数查询
        dto.checkParam();
        try {
            WmUser user = WmThreadLocalUtil.getUser();
            log.info("user为:{}", user.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        //分页查询
        Page page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //按敏感词模糊查询
        lambdaQueryWrapper.like(WmSensitive::getSensitives, dto.getName());
        //按时间倒序
        lambdaQueryWrapper.orderByDesc(WmSensitive::getCreatedTime);
        //返回结果
        page = page(page, lambdaQueryWrapper);
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    /**
     * 根据id删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult delSensitiveById(Integer id) {
        //检查参数
        if (id == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        //删除敏感词
        WmSensitive wmSensitive = new WmSensitive();
        wmSensitive.setId(id);
        removeById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 新增敏感词
     *
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult saveSensitive(WmSensitive wmSensitive) {
        //检查参数
        if (wmSensitive == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        if (StringUtils.isBlank(wmSensitive.getSensitives()))
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        //设置参数
        wmSensitive.setCreatedTime(new Date());
        //新增到数据库
        save(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 更新敏感词
     *
     * @param dto
     * @return
     */
    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;
    @Override
    public ResponseResult updateSensitive( AdSensitive dto) {
        //检查参数
        if(dto == null){
            return ResponseResult.errorResult(400,"参数错误");
        }
        WmSensitive wmSensitive = new WmSensitive();
        BeanUtils.copyProperties(dto,wmSensitive);
        updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
